﻿using System;
using System.Collections.Generic;
using System.Windows.Media.Animation;
using netDxf;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Tables;

namespace TestDxfDocument.toolingsystem
{
    /// <summary>
    /// ISO 13399-406
    /// </summary>
    public class ModellingofConnectionInterface
    {
        public static void HSKHollowTaperInterfaceWithFlangeContact(Dictionary<string, Object> data)
        {
            DxfDocument dxf = new DxfDocument();
            
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer center = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓

            double pi = Math.PI;

            String type = Convert.ToString(data["type"]);
            var HTID = HollowTaperInterfaceDimensions.getparam(type);
            double b1 = HTID.B1;
            double l1 = HTID.L1;
            double l2 = HTID.L2;
            double l4 = HTID.L4;
            double l8 = HTID.L8;
            double l12 = HTID.L12;
            double d1 = HTID.D1;
            double d2 = HTID.D2;
            double d8 = HTID.D8;
            double d10 = HTID.D10;
            double f1 = HTID.F1;
            double f3 = HTID.F3;
            double f4 = HTID.F4;
            double r1 = HTID.R1;
            double r2 = HTID.R2;
            double r9 = HTID.R9;
            
            double angle = 2.86806 * pi / 180; //2°52′5″
            string path = Convert.ToString(data["path"]);

            Vector2 p0 = new Vector2(0, 0);
            Vector2 p1 = fun.Polar(p0, pi, l1);
            Vector2 p2 = new Vector2(p1.X, p0.Y + d2 / 2 - (l1 - l2) * Math.Tan(angle));
            Vector2 p3 = new Vector2(p0.X - l2, p0.Y + d2 / 2);
            Vector2 p4 = fun.Polar(p3, 0, l2 * 0.75);
            Vector2 p4c = fun.Polar(p4, 0, l2 * 0.125);
            Vector2 p5 = fun.Polar(p4c, 0, l2 * 0.125);
            Vector2 p6 = new Vector2(p0.X, p0.Y + d1 / 2);
            Vector2 p9m = new Vector2(p0.X + f3, p0.Y + d10 / 2);
            Vector2 p9 = fun.Polar(p9m, pi, f4 / 2);
            Vector2 p8 = fun.Polar(p9, 0.5 * pi, r1);
            Vector2 p7 = new Vector2(p8.X - (d1 / 2 - d10 / 2 - r1) * Math.Tan(pi / 6), p6.Y);
            Vector2 p10 = fun.Polar(p9m, 0, f4 / 2);
            Vector2 p11 = new Vector2(p10.X, p8.Y);
            Vector2 p12 = new Vector2(p11.X + (p8.X - p7.X), p7.Y);
            Vector2 p13 = new Vector2(p0.X + f1, p6.Y);
            
            Vector2 p2p = commonfun.pointSymmetryOfAxis(p2, "x");
            Vector2 p3p = commonfun.pointSymmetryOfAxis(p3, "x");
            Vector2 p4p = commonfun.pointSymmetryOfAxis(p4, "x");
            Vector2 p4cp = commonfun.pointSymmetryOfAxis(p4c, "x");
            Vector2 p5p = commonfun.pointSymmetryOfAxis(p5, "x");
            Vector2 p6p = commonfun.pointSymmetryOfAxis(p6, "x");
            Vector2 p7p = commonfun.pointSymmetryOfAxis(p7, "x");
            Vector2 p8p = commonfun.pointSymmetryOfAxis(p8, "x");
            Vector2 p9p = commonfun.pointSymmetryOfAxis(p9, "x");
            Vector2 p10p = commonfun.pointSymmetryOfAxis(p10, "x");
            Vector2 p11p = commonfun.pointSymmetryOfAxis(p11, "x");
            Vector2 p12p = commonfun.pointSymmetryOfAxis(p12, "x");
            Vector2 p13p = commonfun.pointSymmetryOfAxis(p13, "x");

            LwPolyline outline1 = Pus.AddPointsToLwPolyLine(p2, p3, p4, p4p, p3p, p2p);
            fun.drawBLine(dxf, Pus.GetV2ArrayFromLwPolyline(outline1), 0.25, contour);
            LwPolyline outline2 = Pus.AddPointsToLwPolyLine(p6,p7,p8,p9,p10,p11,p12,p13,p13p,p12p,p11p,p10p,p9p,p8p,p7p,p6p,p6);
            fun.drawBLine(dxf, Pus.GetV2ArrayFromLwPolyline(outline2), 0.25, contour);
            commonfun.arcWithWidth(fun.DrawArc(p4, p4c, p5), dxf, 0.25, contour);
            commonfun.arcWithWidth(fun.DrawArc(p5p, p4cp, p4p), dxf, 0.25, contour);

            Vector2 p20 = new Vector2(p13.X, p0.Y + r9);
            Vector2 p21 = new Vector2(p12.X, p20.Y);
            Vector2 p22 = new Vector2(p11.X, p20.Y);
            Vector2 p23 = new Vector2(p8.X, p20.Y);
            Vector2 p24 = new Vector2(p7.X, p20.Y);
            Vector2 p25 = new Vector2(p20.X - l12 + r9, p20.Y);
            Vector2 p20p = commonfun.pointSymmetryOfAxis(p20, "x");
            Vector2 p22p = commonfun.pointSymmetryOfAxis(p22, "x");
            Vector2 p23p = commonfun.pointSymmetryOfAxis(p23, "x");
            Vector2 p25p = commonfun.pointSymmetryOfAxis(p25, "x");

            LwPolyline poly1 = new LwPolyline();
            poly1.Vertexes.Add(new LwPolylineVertex(p20));
            poly1.Vertexes.Add(new LwPolylineVertex(p25));
            poly1.Vertexes[1].Bulge = 1;
            poly1.Vertexes.Add(new LwPolylineVertex(p25p));
            poly1.Vertexes.Add(new LwPolylineVertex(p20p));
            fun.PolyFill(dxf, poly1, 0.25, contour);
            
            Vector2[] outline3 = {p7, p24};
            commonfun.drawLineWithWidth(outline3,dxf,0.25,contour);
            Vector2[] outline4 = {p9, p23};
            commonfun.drawLineWithWidth(outline4,dxf,0.25,contour);
            Vector2[] outline5 = {p10, p22};
            commonfun.drawLineWithWidth(outline5,dxf,0.25,contour);
            Vector2[] outline6 = {p12, p21};
            commonfun.drawLineWithWidth(outline6,dxf,0.25,contour);
            Vector2[] outline7 = {p10p, p22p};
            commonfun.drawLineWithWidth(outline7,dxf,0.25,contour);
            Vector2[] outline8 = {p9p, p23p};
            commonfun.drawLineWithWidth(outline8,dxf,0.25,contour);
            
            Line cenLine1 = new Line(fun.Polar(p0, pi, l1 + 1), fun.Polar(p0, 0, f1 + 1));
            cenLine1.Linetype = Linetype.Center;
            cenLine1.Layer = center;
            dxf.AddEntity(cenLine1);
            Line cenLine2 = new Line(new Vector2(p9m.X, d1 / 2 + 1), new Vector2(p9m.X, -d1 / 2 - 1));
            cenLine2.Linetype = Linetype.Center;
            cenLine2.Layer = center;
            dxf.AddEntity(cenLine2);

            Vector2 cp1 = new Vector2(p0.X - l8, p0.Y);
            Circle circle1 = new Circle(cp1, d8 / 2);
            commonfun.circleWithWidth(circle1, dxf, 0.25, contour);
            // Vector2 cp2 = new Vector2(p9m.X, p0.Y + d11 / 2);
            // Circle circle2 = new Circle(cp2, d12 / 2);
            // commonfun.circleWithWidth(circle2, dxf, 0.1, contour);

            Vector2 p30 = new Vector2(p13.X, p0.Y - d1 / 4);
            Vector2 p31 = new Vector2(p10.X, p30.Y);
            Vector2 p32 = new Vector2(p9.X, p30.Y);
            Vector2 p33 = new Vector2(p25.X, p30.Y);
            Vector2 p34 = new Vector2(p13.X - l12, p30.Y - r9);
            Vector2 p35 = new Vector2(p7.X, p34.Y);
            Vector2 p38 = new Vector2(p12.X, p34.Y);
            Vector2 p39 = new Vector2(p13.X, p34.Y);
            
            Vector2[] outline9 = {p30,p31,p38,p39};
            commonfun.drawLineWithWidth(outline9,dxf,0.25,contour);
            Vector2[] outline10 = {p38,p12p};
            commonfun.drawLineWithWidth(outline10,dxf,0.25,contour);
            Vector2[] outline11 = {p34, p35, p32, p33};
            commonfun.drawLineWithWidth(outline11,dxf,0.25,contour);
            Vector2[] outline12 = {p35, p7p};
            commonfun.drawLineWithWidth(outline12,dxf,0.25,contour);
            Arc arc1 = fun.DrawArc(p33, new Vector2(p33.X, p34.Y), p34);
            commonfun.arcWithWidth(arc1,dxf,0.25,contour);

            Vector2 p40 = new Vector2(p2.X, p2.Y * Math.Sin(27 * pi / 180));
            Vector2 p41 = new Vector2(p40.X + l4, p40.Y + l4 * Math.Tan(pi / 72));
            Vector2 p42 = new Vector2(p40.X, p40.Y - (p40.Y - b1 / 2) * 0.65);
            Vector2 p43 = fun.Polar(p40, pi / 72, l4 / 8);
            Vector2 p44 = new Vector2(p43.X, b1 / 2);
            Vector2 p45 = new Vector2(p40.X + l4 - r2, p44.Y);
            Vector2 p46 = new Vector2(p40.X + l4, p45.Y - r2);
            Vector2 p47 = new Vector2(p40.X + 1.5 * l4, p45.Y);
            Vector2 p48 = new Vector2(p47.X + r2, p47.Y - r2);

            Vector2 p40p = commonfun.pointSymmetryOfAxis(p40, "x");
            Vector2 p41p = commonfun.pointSymmetryOfAxis(p41, "x");
            Vector2 p45p = commonfun.pointSymmetryOfAxis(p45, "x");
            Vector2 p46p = commonfun.pointSymmetryOfAxis(p46, "x");
            Vector2 p47p = commonfun.pointSymmetryOfAxis(p47, "x");
            Vector2 p48p = commonfun.pointSymmetryOfAxis(p48, "x");

            Vector2[] outline13 = {p2, p40, p41, p41p, p40p, p2p};
            commonfun.drawLineWithWidth(outline13,dxf,0.25,contour);
            Vector2[] outline14 = {p40, p42, p44, p45};
            commonfun.drawLineWithWidth(outline14,dxf,0.25,contour);
            commonfun.drawRotateXLineWithWidth(outline14,dxf,0.25,contour);
            Arc arc2 = fun.DrawArc(p46, new Vector2(p45.X, p46.Y), p45);
            commonfun.arcWithWidth(arc2,dxf,0.25,contour);
            Arc arc3 = fun.DrawArc(p45p, new Vector2(p45p.X,p46p.Y), p46p);
            commonfun.arcWithWidth(arc3,dxf,0.25,contour);

            Line line1 = new Line(p45, p47);
            line1.Layer = refin;
            line1.Linetype = Linetype.Dashed;
            dxf.AddEntity(line1);
            Line line2 = new Line(p45p, p47p);
            line2.Layer = refin;
            line2.Linetype = Linetype.Dashed;
            dxf.AddEntity(line2);
            Arc arc4 = fun.DrawArc(p48, new Vector2(p47.X, p48.Y), p47);
            arc4.Layer = refin;
            arc4.Linetype = Linetype.Dashed;
            dxf.AddEntity(arc4);
            Arc arc5 = fun.DrawArc(p47p, new Vector2(p47p.X, p48p.Y), p48p);
            arc5.Layer = refin;
            arc5.Linetype = Linetype.Dashed;
            dxf.AddEntity(arc5);
            Line line3 = new Line(p48, p48p);
            line3.Layer = refin;
            line3.Linetype = Linetype.Dashed;
            dxf.AddEntity(line3);

            DimensionStyle ds = new DimensionStyle("sd");
            ds.ArrowSize = 1.6;
            ds.DimArrow1 = DimensionArrowhead.Closedfilled;
            ds.DimArrow2 = DimensionArrowhead.Closedfilled;
            ds.TextHeight = 1;
            double offset = 10;

            LinearDimension dim1 = commonfun.LinearDimension(p6, p2, offset, 0, ds);
            dim1.UserText = String.Format("L1");
            dxf.AddEntity(dim1);
            LinearDimension dim2 = commonfun.LinearDimension(p6, p3, offset * 0.5, 0, ds);
            dim2.UserText = String.Format("L2");
            dxf.AddEntity(dim2);
            LinearDimension dim3 = commonfun.LinearDimension(p6, p13, offset * 1.5, 0, ds);
            dim3.UserText = String.Format("f1");
            dxf.AddEntity(dim3);
            LinearDimension dim4 = commonfun.LinearDimension(p6, p9m, offset, 0, ds);
            dim4.UserText = String.Format("f3");
            dxf.AddEntity(dim4);
            LinearDimension dim5 = commonfun.LinearDimension(p9,p10, offset, 180, ds);
            dim5.UserText = string.Format("f4");
            dxf.AddEntity(dim5);
            LinearDimension dim6 = commonfun.LinearDimension(p3, p3p, offset * 2 + p3.X + -p2.X, 90, ds);
            dim6.UserText = string.Format("d1");
            dxf.AddEntity(dim6);
            LinearDimension dim7 = commonfun.LinearDimension(p2, p2p, offset, 90, ds);
            dim7.UserText = string.Format("d2");
            dxf.AddEntity(dim7);
            LinearDimension dim8 = commonfun.LinearDimension(p10, p10p, offset * 0.8 + p13.X - p10.X, 270, ds);
            dim8.UserText = string.Format("d10");
            dxf.AddEntity(dim8);

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            double scale = Convert.ToDouble(data["scale"]); //放大系数
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}